#include <iostream>
using namespace std;

//赋值运算符 ： C++编译器自己会有提供 operator=() ， 但是当有成员属性创建在堆区上时，进行赋值时会出现深浅拷贝的问题

class Person {
public:
    int *m_age;
    Person(int age) {
        m_age = new int(age);
    }

    ~Person() {
        if (m_age != nullptr) {
            delete m_age;
            m_age = nullptr;
        }
    }

    Person & operator=(const Person & p) {
        if (m_age != nullptr) {
            delete m_age;
            m_age = nullptr;
        }
        m_age = new int(*p.m_age);
        return *this;
    }

};

int main() {
    Person p1(18);
    Person p2(20);
    cout << "p1 age:" << *p1.m_age << endl;
    cout << "p2 age:" << *p2.m_age << endl;

    p2 = p1;
    cout << "p1 age:" << *p1.m_age << endl;
    cout << "p2 age:" << *p2.m_age << endl;
    Person p3(30);
    cout << "p3 age:" << *p3.m_age << endl;

    p3 = p2 = p1;
    cout << "p1 age:" << *p1.m_age << endl;
    cout << "p2 age:" << *p2.m_age << endl;
    cout << "p3 age:" << *p3.m_age << endl;
    return 0;
}